

/**
 * 通用js方法封装处理
 * Copyright (c) 2019 ruoyi
 */

interface FormatObj {
  y: number;
  m: number;
  d: number;
  h: number;
  i: number;
  s: number;
  a: number;
}

interface TreeConfig {
  id: string;
  parentId: string;
  childrenList: string;
}



interface SearchParams {
  params: Record<string, any>;
  [key: string]: any;
}

// 添加日期范围
export function addDateRange(params: SearchParams, dateRange: string[], propName?: string): SearchParams {
  let search = params;
  search.params = typeof (search.params) === 'object' && search.params !== null && !Array.isArray(search.params) ? search.params : {};
  dateRange = Array.isArray(dateRange) ? dateRange : [];
  if (typeof (propName) === 'undefined') {
    search.params['beginTime'] = dateRange[0];
    search.params['endTime'] = dateRange[1];
  } else {
    search.params['begin' + propName] = dateRange[0];
    search.params['end' + propName] = dateRange[1];
  }
  return search;
}

interface DictData {
  value: string | number;
  label: string;
  [key: string]: any;
}

// 回显数据字典
export function selectDictLabel(datas: Record<string, DictData>, value: string | number | undefined): string {
  if (value === undefined) {
    return "";
  }
  let actions: string[] = [];
  Object.keys(datas).some((key) => {
    if (datas[key].value == ('' + value)) {
      actions.push(datas[key].label);
      return true;
    }
  });
  if (actions.length === 0) {
    actions.push(String(value));
  }
  return actions.join('');
}

// 字符串格式化(%s )
export function sprintf(str: string, ...args: any[]): string {
  let flag = true, i = 1;
  str = str.replace(/%s/g, function () {
    let arg = args[i++];
    if (typeof arg === 'undefined') {
      flag = false;
      return '';
    }
    return arg;
  });
  return flag ? str : '';
}

// 转换字符串，undefined,null等转化为""
export function parseStrEmpty(str: string | null | undefined): string {
  if (!str || str == "undefined" || str == "null") {
    return "";
  }
  return str;
}

// 数据合并
export function mergeRecursive(source: Record<string, any>, target: Record<string, any>): Record<string, any> {
  for (let p in target) {
    try {
      if (target[p].constructor == Object) {
        source[p] = mergeRecursive(source[p], target[p]);
      } else {
        source[p] = target[p];
      }
    } catch (e) {
      source[p] = target[p];
    }
  }
  return source;
}

/**
 * 构造树型结构数据
 * @param {*} data 数据源
 * @param {*} id id字段 默认 'id'
 * @param {*} parentId 父节点字段 默认 'parentId'
 * @param {*} children 孩子节点字段 默认 'children'
 */
// export function handleTree(data: Record<string, any>[], id?: string, parentId?: string, children?: string): Record<string, any>[] {
//   let config: TreeConfig = {
//     id: id || 'id',
//     parentId: parentId || 'parentId',
//     childrenList: children || 'children'
//   };

//   const childrenListMap: Record<string, Record<string, any>[]> = {};
//   const nodeIds: Record<string, Record<string, any>> = {};
//   const tree: Record<string, any>[] = [];

//   for (let d of data) {
//     let parentId = d[config.parentId];
//     if (childrenListMap[parentId] == null) {
//       childrenListMap[parentId] = [];
//     }
//     nodeIds[d[config.id]] = d;
//     childrenListMap[parentId].push(d);
//   }

//   for (let d of data) {
//     let parentId = d[config.parentId];
//     if (nodeIds[parentId] == null) {
//       tree.push(d);
//     }
//   }

//   for (let t of tree) {
//     adaptToChildrenList(t);
//   }

//   function adaptToChildrenList(o: Record<string, any>): void {
//     if (childrenListMap[o[config.id]] !== null) {
//       o[config.childrenList] = childrenListMap[o[config.id]];
//     }
//     if (o[config.childrenList]) {
//       for (let c of o[config.childrenList]) {
//         adaptToChildrenList(c);
//       }
//     }
//   }
//   return tree;
// }

/**
* 参数处理
* @param {*} params  参数
*/
export function tansParams(params: Record<string, any>): string {
  let result = '';
  for (const propName of Object.keys(params)) {
    const value = params[propName];
    let part = encodeURIComponent(propName) + "=";
    if (value !== null && value !== "" && typeof (value) !== "undefined") {
      if (typeof value === 'object') {
        for (const key of Object.keys(value)) {
          if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
            let params = propName + '[' + key + ']';
            let subPart = encodeURIComponent(params) + "=";
            result += subPart + encodeURIComponent(value[key]) + "&";
          }
        }
      } else {
        result += part + encodeURIComponent(value) + "&";
      }
    }
  }
  return result;
}

// 返回项目路径
export function getNormalPath(p: string): string {
  if (p.length === 0 || !p || p == 'undefined') {
    return p;
  }
  let res = p.replace('//', '/');
  if (res[res.length - 1] === '/') {
    return res.slice(0, res.length - 1);
  }
  return res;
}

// 验证是否为blob格式
export function blobValidate(data: any): boolean {
  return data.type !== 'application/json';
}
  